﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NovoSerDes.Forms;
using Sunny.UI;
using NovoSerDesEva;
using System.Collections;

namespace NovoSerDes
{
    public partial class OtherTools : UIPage
    {
        private SerDesComm commDev;
        string selectSerComm;

        public OtherTools()
        {
            InitializeComponent();
        }

        private void uiLinkLabel_EOM_Click(object sender, EventArgs e)
        {
            if (commDev == null || !commDev.IsOpen)
            {
                UIMessageBox.ShowError("CH347未打开", true, true); 
                return;
            }
            Form_EOM frm_EOM = new Form_EOM(commDev);
            frm_EOM.Show();
        }

        private void uiLinkLabel_EFUSE_Click(object sender, EventArgs e)
        {
            if (commDev == null || !commDev.IsOpen)
            {
                UIMessageBox.ShowError("CH347未打开", true, true);
                return;
            }
            Form_EFUSE frm_EFUSE = new Form_EFUSE(commDev);
            frm_EFUSE.ShowDialog();
        }

        private void uiComboBox_seclectSerComm_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (uiComboBox_seclectSerComm.SelectedIndex)
            {
                case 0:
                    selectSerComm = "NLS9246";
                    break;
                case 1:
                    selectSerComm = "NLS9116A";
                    break;
                case 2:
                    selectSerComm = "NLS9116B";
                    break;
                case 3:
                    selectSerComm = "NLS9116C";
                    break;
                case 4:
                    selectSerComm = "NLS9116D";
                    break;
            }
            setComm();
        }

        private void setComm()
        {
            Dictionary<string, SerDesComm> pairs = MainForm.IniSerCommPairs;
            if (pairs.ContainsKey(selectSerComm))
            {
                commDev = pairs[selectSerComm];
                Console.WriteLine("选择了{0}", selectSerComm);
            }
            else
            {
                commDev = null;
                Console.WriteLine("commDev = null");
            }

            CheckCommDevIsOpen();
        }

        private void CheckCommDevIsOpen()
        {
            if (commDev == null || !commDev.IsOpen)
            {
                UIMessageBox.ShowError("CH347未打开", true, true);
                return;
            }
        }


        private void CheckSerCommPairs()
        {
            Dictionary<string, SerDesComm> serCommPairs = MainForm.IniSerCommPairs;
            if (serCommPairs.Count == 0)
            {
                Console.WriteLine("serCommPairs键值对个数为0");
            }
            else
            {
                Console.WriteLine("serCommPairs键值对个数为{0}", serCommPairs.Count);
                foreach (KeyValuePair<string, SerDesComm> kvp in serCommPairs)
                {
                    Console.WriteLine($"Key = {kvp.Key}, Value = {kvp.Value}");
                }
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            CheckSerCommPairs();
        }
    }
}
